/**
 * 
 */
package com.roncoo.adminlte.controller.admin;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.roncoo.adminlte.bean.Result;
import com.roncoo.adminlte.bean.entity.RcAccess;
import com.roncoo.adminlte.bean.entity.RcDataDictionary;
import com.roncoo.adminlte.biz.AccessBiz;
import com.roncoo.adminlte.util.base.BaseController;
import com.roncoo.adminlte.util.base.Page;
import com.roncoo.adminlte.util.base.PageBean;

/**
 * @author zhangliping
 *
 */
@Controller
@RequestMapping(value = "/admin/access", method = RequestMethod.POST)
public class AccessController extends BaseController {

	@Autowired
	private AccessBiz biz;
	
	@RequestMapping(value = LIST, method = RequestMethod.GET)
	public void list(@RequestParam(value = "id", defaultValue = "-1") Long id, @RequestParam(value = "fieldCode", defaultValue = "") String fieldCode) {
	}
	
	/**
	 * 添加
	 */
	@RequestMapping(value = ADD, method = RequestMethod.GET)
	public void add() {
		
	}
	
	
	/**
	 * 分页查询
	 * 
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = PAGE)
	public PageBean<RcAccess> queryForPage(@RequestParam(value = "start", defaultValue = "1") int start, @RequestParam(value = "length", defaultValue = "10") int pageSize, @RequestParam(value = "date", required = false) String date, @RequestParam(value = "search", required = false) String search) {
		Result<Page<RcAccess>> result = biz.listForPage((start / pageSize) + 1, pageSize, search, date);
		if (result.isStatus()) {
			PageBean<RcAccess> pageBean = new PageBean<RcAccess>();
			Page<RcAccess> newPageBean = result.getResultData();
			
			try {
				BeanUtils.copyProperties(newPageBean, pageBean);
				pageBean.setData(newPageBean.getList());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			
			return pageBean;
		}
		return new PageBean<RcAccess>();
	}
	
	/**
	 * 
	 * 往数据字典中插入记录
	 * 
	 * @param dictionary
	 * @return String
	 */
	@RequestMapping(value = SAVE)
	public String save(@ModelAttribute RcAccess rcAccess, ModelMap modelMap) {
		Result<RcAccess> result = biz.save(rcAccess);
		if (result.isStatus()) {
			return redirect("/admin/access/list");
		}
		return "admin/access/list";
	}
	

	/**
	 * 删除
	 * 
	 * @param id
	 * @param fieldCode
	 * @return
	 */
	@RequestMapping(value = DELETE, method = RequestMethod.GET)
	public String delete(@RequestParam(value = "id", defaultValue = "-1") Long id, @RequestParam(value = "fieldCode", defaultValue = "") String fieldCode) {
		Result<String> result = biz.delete(id, fieldCode);
		if (result.isStatus()) {
			return redirect("/admin/dataDictionary/list");
		}
		return redirect("/admin/dataDictionary/list");
	}

	/**
	 * 查看详细信息
	 * 
	 * @param id
	 * @param modelMap
	 */
	@RequestMapping(value = VIEW, method = RequestMethod.GET)
	public void view(@RequestParam(value = "id", defaultValue = "-1") Long id, ModelMap modelMap) {
		Result<RcAccess> result = biz.query(id);
		if (result.isStatus()) {
			modelMap.put("access", result.getResultData());
		}
	}

	/**
	 * 编辑
	 * 
	 * @param id
	 * @param modelMap
	 */
	@RequestMapping(value = EDIT, method = RequestMethod.GET)
	public void edit(Long id, ModelMap modelMap) {
		Result<RcAccess> result = biz.query(id);
		if (result.isStatus()) {
			modelMap.put("access", result.getResultData());
		}
	}

	/**
	 * 更新
	 * 
	 * @param dictionary
	 * @param oldFieldCode
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = UPDATE)
	public String update(@ModelAttribute RcAccess access, @RequestParam(value = "id") Long id) {
		Result<RcAccess> result = biz.update(access, id);
		if (result.isStatus()) {
			// 更新之后，提示都没有
			return redirect("/admin/access/list");
		}
		return "admin/access/list";

	}
	
}
